<template>
  <!-- //日志管理 -->
  <div>
    <!--<div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-setting"></i> 日志管理</el-breadcrumb-item>
                  <el-breadcrumb-item>{{$route.meta.displayName}}</el-breadcrumb-item>
            </el-breadcrumb>
        </div>-->

    <el-form :inline="true" :model="search" ref="search">
      <el-form-item label="关键词:" prop="keywords">
        <el-input v-model="search.keywords" style="width:250px" auto-complete placeholder="可输入用户,IP地址,编号,描述,细节"></el-input>
      </el-form-item>
      <el-form-item prop="resource">
        <el-select v-model="search.resource" style="width:120px" placeholder="全部资源">
          <el-option v-for="item in resourceOptions()" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="action">
        <el-select v-model="search.action" style="width:120px" placeholder="全部动作">
          <el-option v-for="item in actionOptions()" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="timeRangeArr">
        <el-date-picker range-separator="-" v-model="search.timeRangeArr" type="daterange" placeholder="选择日期范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchClick">查询</el-button>
        <el-button @click="resetForm('search')">重置搜索</el-button>
      </el-form-item>
    </el-form>

    <v-table :options="zabbixLogData" @refreshActionHandle="refreshActionHandle" :border="true"></v-table>
  </div>
</template>

<script>
import vTable from 'components/Table.vue'
import LogService from 'api/config/logmanager'
import ResTypeEnum from 'common/resTypeEnum'
import Utils from 'utils/filters'
export default {
  components: {
    vTable
  },
  data: function() {
    return {
      pageIndex: 1,
      pageSize: 10,
      //获取资源选项
      resourceOptions() {
        let resour = ResTypeEnum.getAuditResource();
        let res = [];
        for (let key in resour) {
          res.push({ label: resour[key], value: key });
        }
        res.sort(function(a, b) {
          return a.label - b.label;
        });
        return res;
      },
      //获取动作选项
      actionOptions() {
        let actions = ResTypeEnum.getAuditAction();
        let res = [];
        for (let key in actions) {
          res.push({ label: actions[key], value: key });
        }
        res.sort(function(a, b) {
          return a.label - b.label;
        });
        return res;
      },
      search: {
        keywords: '',
        timeRange: '',
        timeRangeArr: '',
        resource: '',
        action: '',
        timeFormatter: 'yyyy-MM-dd'
      },
      userLogData: {
        data: [],
        columns: [{

        }],
        hasPagination: true
      },//用户操作日志
      zabbixLogData: {
        data: [],
        columns: [
          {
            prop: 'clock',
            label: 'time',
            formatter: function(row, column) {
              // console.log(row.clock);
              return Utils.formatDate(row.clock * 1000);
            },
            // minWidth:'100'
          },
          {
            prop: 'alias',
            label: 'user',
            // minWidth:'70'
          },
          {
            prop: 'ip',
            label: 'IP address',
            // minWidth:'100'
          },
          {
            prop: 'resourcetype',
            label: 'resource',
            formatter: function(row, column) {
              return ResTypeEnum.getAuditResourceToStr(row.resourcetype);
            },
            // minWidth:'80'
          },
          {
            prop: 'action',
            label: 'action',
            formatter: function(row, column) {
              return ResTypeEnum.getAuditActionToStr(row.action);
            },
            // minWidth:'80'
          },
          {
            prop: 'resourceid',
            label: 'ID',
            // minWidth:'80'
          },
          {

            prop: 'resourcename',
            label: 'describe',
            // minWidth:'100'
          },
          {
            prop: 'details',
            label: 'detail',
            formatter: function(row, column) {
              return row.details == '0' ? "" : row.details;
            },
            // minWidth:'100'
          }
        ],
        hasPagination: true,
        exportExc: {
          hasPagination: true,
          headers: [],
          fileName: '日志信息.xlsx',
          dataFunc: function() {
          },
          params: [],
          dataFunc2: function() {
          },
          params2: []
        },
      },//zabbix 操作日志
    };
  },
  methods: {
    pageQueryAuditlogs(params, pageIndex, pageSize) {
      let self = this;
      LogService.pageQueryAuditlogs(params, pageIndex, pageSize).then(response => {
        self.zabbixLogData.data = response.data.rows;
        self.zabbixLogData.total = response.data.total;
        self.zabbixLogData.pageIndex = response.data.pageIndex;
        self.zabbixLogData.pageSize = response.data.pageSize;
      }).catch(err => {

      });
    },
    refreshActionHandle(pageIndex, pageSize) {
      let params = {};
      let self = this;
      self.pageIndex = pageIndex
      self.pageSize = pageSize
      LogService.pageQueryAuditlogs(params, pageIndex, pageSize).then(response => {
        self.zabbixLogData.total = response.data.total;
        self.zabbixLogData.pageIndex = response.data.pageIndex;
        self.zabbixLogData.pageSize = response.data.pageSize;
        self.zabbixLogData.data = response.data.rows;

      }).catch(err => {

      });
    },
    searchClick() {
      let self = this;
      if (self.search.timeRangeArr.length > 0) {
        self.search.timeRange = Utils.formatDateDay(self.search.timeRangeArr[0]) + ',' + Utils.formatDateDay(self.search.timeRangeArr[1]);
      }
      let old = self.search.timeRangeArr;
      self.search.timeRangeArr = '';
      self.pageQueryAuditlogs(self.search, self.pageIndex, self.pageSize);
      self.search.timeRangeArr = old;
      self.changeExcelParams()
    },
    resetForm(formName) {
      let self = this;
      self.$refs[formName].resetFields();
      self.search.timeRange = '';
    },
    //导出excel时用的参数
    changeExcelParams() {
      let self = this;
      self.zabbixLogData.exportExc.hasPagination = true
      let header = []
      for (let item of self.zabbixLogData.columns) {
        if(item.prop){
          header.push({ name: self.$t(item.label), v: item.prop })
        }
      }
      self.zabbixLogData.exportExc.headers = header
      self.zabbixLogData.exportExc.fileName = '操作日志信息.xlsx'
      self.zabbixLogData.exportExc.dataFunc = LogService.getAllZabbixAuditLog
      self.zabbixLogData.exportExc.params = []
      self.zabbixLogData.exportExc.dataFunc2 = LogService.pageQueryAuditlogs
      self.zabbixLogData.exportExc.params2 = [self.search, self.pageIndex, self.pageSize]
    }
  },
  mounted: function() {
    this.searchClick();
  }
}
</script>

<style>

</style>
